﻿using SIR.Common;
using SIR.Common.Condition;
using SIR.Entities.Interfaces;
using System.Linq;

namespace SIR.Entities.Implementations
{
	/// <summary>
	/// Defines a summary of the spectrum given a section of the image and a specific color definition
	/// </summary>
	public class ColorSpectrum : Entity, IColorSpectrum
	{
		/// <summary>
		/// Creates a new instance of a Color Spectrum
		/// </summary>
		public ColorSpectrum()
			: base()
		{

		}

		/// <summary>
		/// Creates a color spectrum with a specific color definition
		/// </summary>
		/// <param name="definition"></param>
		public ColorSpectrum(IColorSpectrumDefinition definition)
			: this()
		{
			DefinitionId = definition != null ? definition.Id : 0;
		}

		private IColorSpectrumDefinition _definition = null;

		/// <summary>
		/// Gets or sets the average distance for the color spectrum
		/// </summary>
		[Common.DatabaseAccess(DatabaseAccessRights.ReadWrite)]
		public double AverageDistance { get; set; }
		/// <summary>
		/// Gets or sets the percentange of the section that is within the distance to the color
		/// </summary>
		[Common.DatabaseAccess(DatabaseAccessRights.ReadWrite)]
		public double PercentWithinDistance { get; set; }
		/// <summary>
		/// Gets or sets the average hue distances
		/// </summary>
		[Common.DatabaseAccess(DatabaseAccessRights.ReadWrite)]
		public double AverageHueDistance { get; set; }
		/// <summary>
		/// Gets or sets the horizontal section of the image to which the spectrum is from
		/// </summary>
		[Common.DatabaseAccess(DatabaseAccessRights.ReadWrite)]
		public int SectionX { get; set; }
		/// <summary>
		/// Gets or sets the vertical section of the image to which the spectrum is from
		/// </summary>
		[Common.DatabaseAccess(DatabaseAccessRights.ReadWrite)]
		public int SectionY { get; set; }
		/// <summary>
		/// Gets or sets the identifier for the color definition
		/// </summary>
		[Common.DatabaseAccess(DatabaseAccessRights.ReadWrite)]
		public long DefinitionId { get; set; }
		/// <summary>
		/// Gets or sets the image spectrum identifier
		/// </summary>
		[Common.DatabaseAccess(DatabaseAccessRights.ReadWrite)]
		public long ImageAnalysisId { get; set; }
		/// <summary>
		/// Gets the color spectrum definition
		/// </summary>
		[Newtonsoft.Json.JsonIgnore]
		[Common.DatabaseAccess(DatabaseAccessRights.None)]
		public IColorSpectrumDefinition Definition
		{
			get
			{
				if (_definition == null)
				{
					_definition = DAL.DataServer.Find<ColorSpectrumDefinition>(new Predicate("Id", DefinitionId, PredicateOperator.EqualTo)).Results.FirstOrDefault();
				}

				return _definition;
			}
		}
	}
}

